/*
 * Copyright (C) 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.androidwearmap;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.GroundOverlay;
import com.google.android.gms.maps.model.GroundOverlayOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.support.wearable.view.DismissOverlayView;
import android.util.Log;
import android.view.View;
import android.view.WindowInsets;
import android.widget.FrameLayout;
import android.widget.Toast;

/**
 * Sample that shows how to set up a basic Google Map on Android Wear.
 */
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

    private LatLng SYDNEY = new LatLng(0, 0);
    private PositionReceiver positionReceiver;
    private Context context=this;
    /**
     * The map. It is initialized when the map has been fully loaded and is ready to be used.
     *
     * @see #onMapReady(com.google.android.gms.maps.GoogleMap)
     */
    private GoogleMap mMap;

    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        setContentView(R.layout.activity_main);
        SupportMapFragment mapFragment =
                (SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        positionReceiver = new PositionReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.androidwearmap.RECEIVER");
        registerReceiver(positionReceiver, intentFilter);

      	Intent gps=new Intent(this,GpsService.class);
     	this.startService(gps);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(positionReceiver);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
//        mMap.addMarker(new MarkerOptions().position(SYDNEY)
//                .title("Sydney Opera House"));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, 15));
    }

    double latitude;
    double longitude;
    public  class  PositionReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            latitude=intent.getDoubleExtra("latitude",0);
            longitude=intent.getDoubleExtra("longitude",0);
            Toast.makeText(context,"经纬度"+latitude+","+longitude,Toast.LENGTH_SHORT).show();
            SYDNEY=new LatLng(latitude,longitude);
            mMap.addMarker(new MarkerOptions().position(SYDNEY)
                    .title("Sydney Opera House").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher)));


            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, 15));




//            GroundOverlayOptions newarkMap = new GroundOverlayOptions()
//                    .image(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher))
//                    .position(SYDNEY, 100f, 65f);
//            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, 15));
//            mMap.addGroundOverlay(newarkMap);



        }
    }
}
